package com.atguigu;

import java.security.PrivateKey;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @ClassName SaleTicketDemo
 * @Author yy
 * @create 2022/9/8 20:44
 */

class Ticket{
    public int getTicket() {
        return ticket;
    }

    private int ticket = 50;

    private Lock lock = new ReentrantLock();

    public void sale(){
        lock.lock();
        try {
            if (ticket > 0){
                System.out.println(Thread.currentThread().getName()+"号售票员\t 正在卖第"+(ticket--)+"张票");
            }
        } finally {
            lock.unlock();
        }
    }
}
public class SaleTicketDemo {
    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        ExecutorService pool = Executors.newFixedThreadPool(3);
        try {
            for (int i = 0; i < ticket.getTicket(); i++) {
                pool.execute(() -> {
                    ticket.sale();
                });
            }
        } finally {
            pool.shutdown();
        }
    }
}
